// manager/pages/bindcardreporting/index.js
import { request } from '../../../utils/request';
import util from '../../../common/util';
import WxValidate from '../../../common/WxValidate';
import validateIdent from '../../../common/validateIdent';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        disabled: false,
        userInfo: {},
        startDate: "",
        endDate: "",
        list: [],
        showBusiType: true,
        code: "",
        form: {
            configId: "",
            perCustName: "",
            perCustPhone: "",
            perCustIdcard: "",
            perMarketingDate: "",
            creatorId: "",
            deptId: "",
            userCode: "",
            remark: "",
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            startDate: util.getDateMonthFrist(),
            endDate: util.formatDate(new Date()),
            "form.perMarketingDate": util.formatDate(new Date()),
        })
        wx.setNavigationBarTitle({
            title: options.name 
        })
        this.initValidate();
        if(options.detailId){
            this.setData({
                code: options.code,
                "form.configId": options.id,
                disabled: true,
                showBusiType: true,
            })
            this.getMyBusiReportDetail(options);
        }
        this.getUserInfo();
        this.getReportConfigList();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },
    // 用户信息
    getUserInfo(e) {   
        request({
            url: "/user/getUserInfo",
            reqRSAENCRY: true, // 参数是否加密
            resRSADECRY: true, // 返回值是否解密
            data: {}
        }).then(res => {
            if(res.code == 0){
                let { form } = this.data;
                form.creatorId = res.data.userInfo.registId;
                form.deptId = res.data.userInfo.deptId;
                form.userCode = res.data.userInfo.userCode;
                this.setData({
                    userInfo: res.data.userInfo,
                    form
                })  
            }
        }).catch(error => {
            // console.log(error)
        })     
    },
    // 获取详情
    getMyBusiReportDetail(params) {   
        request({
            url: "/busi/reportConfig/getMyBusiReportDetail",
            reqRSAENCRY: true, // 参数是否加密
            resRSADECRY: true, // 返回值是否解密
            data: {
                busiType: params.busiType,
                id: params.detailId,
                configId: params.id,
            }
        }).then(res => {
            if(res.code == 0){
                let { form } = this.data;
                form = Object.assign(form, res.data);
                form.perCustIdcard = form.perCustIdcard&&util.formatCardNum(form.perCustIdcard);
                this.setData({
                    form
                }) 
            }
        }).catch(error => {
            // console.log(error)
        })     
    },
    /**
     * 获取字典
     */
    getReportConfigList() {
        request({
            url: "/busi/reportConfig/getReportConfigList",
            reqRSAENCRY: true, // 参数是否加密
            resRSADECRY: true, // 返回值是否解密
            data: {
				dictType: "ecard_business_reporting_sort",
				busiType: 2
			}
        }).then(res => {
            console.log(res)
            if(res.code == 0){
                let list = res.data.map(item => {
                    item.isDisabled = false;
                    return item
                })
                this.setData({
                    list
                })
            }
        }).catch(error => {
            // console.log(error)
        })     
    },
    /**
     * 查询已办理的绑卡类业务列表
     */
    getReportCardConfigListByCond(perCustIdcard) {
        request({
            url: "/busi/reportCard/getReportCardConfigListByCond",
            reqRSAENCRY: true, // 参数是否加密
            resRSADECRY: true, // 返回值是否解密
            data: {
				perCustIdcard
			}
        }).then(res => {
            console.log(res)
            if(res.code == 0){
                let { list } = this.data;
                let handleList = [];
                res.data.forEach(item => {
                    handleList.push(item.id)
                })
                list.map(item => {
                    if(handleList.indexOf(item.id) != -1 && item.isMore == "F"){
                        item.isDisabled = true;
                    }
                    return item
                })
                this.setData({
                    list
                })
            }
        }).catch(error => {
            // console.log(error)
        })     
    },
    /** 多选项切换 */
    multipleChoicePopupChange(e){
        console.log(e)
        let { formKey, value } = e.detail;
        let arr = [];
        value.forEach(item => {
            arr.push(item.id)
        })
        this.setData({
            ['form.'+formKey]: arr.join(",")
        })
    },
    /**
     * 输入框输入时
     */
    input(e){
        let key = e.target.dataset.key;
        this.setData({
            ['form.'+key]: e.detail.value
        })
    },
    /**
     * 身份证失去焦点
     */
    cardBlur(e){
        console.log(e)
        let key = e.target.dataset.key;
        this.setData({
            showBusiType: false
        })
        if(!validateIdent.IdentityCode_isCardNo(e.detail.value)){
            return
        }
        if(!validateIdent.IdentityCode_checkProvince(e.detail.value)){
            return
        }
        if(!validateIdent.IdentityCode_checkBirthday(e.detail.value)){
            return
        }
        if(!validateIdent.IdentityCode_checkParity(e.detail.value)){
            return
        }
        this.setData({
            showBusiType: true
        })
        this.getReportCardConfigListByCond(e.detail.value);
    },
    /**
     * 日期选择
     */
    bindDateChange: function(e) {
        let { key } = e.currentTarget.dataset;
        this.setData({
            ['form.'+key]: e.detail.value
        })
    },
    // 保存
    save(){
        if(this.data.showBusiType&&!this.data.form.configId){
            wx.showToast({
                title: '请选择业务类型',
                icon: 'none',
                duration: 2000
            });
            return
        }
        // 传入表单数据，调用验证方法
        if (!this.WxValidate.checkForm(this.data.form)) {
            const error = this.WxValidate.errorList[0]
            this.showModal(error)
            return false
        }
        let { form } = this.data;
        request({
            url: "/busi/reportCard/addReportCardInfo",
            reqRSAENCRY: true, // 参数是否加密
            resRSADECRY: false, // 返回值是否解密
            data: form
        }).then(res => {
            if(res.code == 0){
                wx.showToast({
                    title: '保存成功',
                    icon: 'none',
                    mask: true,
                    duration: 2000
                });                
                setTimeout(()=>{
                    wx.navigateBack()
                },2000)
            }
        }).catch(error => {
            // console.log(error)
        })
    },
    // 表单验证
    initValidate() {
        // 验证字段的规则
        const rules = {
            perCustName: {
                required: true,
                name: true
            },
            perCustPhone: {
                required: true,
                tel: true
            },
            perCustIdcard: {
                required: true,
                cardNo: true,
                checkProvince: true,
                checkBirthday: true,
                checkParity: true,
            },
            configId: {
                required: true,
            },
            perMarketingDate: {
                required: true,
            },
        }
        // 验证字段的提示信息，若不传则调用默认的信息
        const messages = {
            perCustName: {
                required: '请输入客户姓名',
            },
            perCustPhone: {
                required: '请输入联系方式',
                tel: '请输入正确的联系方式'
            },
            perCustIdcard: {
                required: '请输入身份证号',
                // idcard: '请输入正确的身份证号'
            },
            configId: {
                required: '请选择业务类型',
            },
            perMarketingDate: {
                required: '请选择营销日期'
            },
        }
        this.WxValidate = new WxValidate(rules, messages)
        // 自定义验证规则
        this.WxValidate.addMethod('name', (value, param) => {
            var returnVal = true;
            if(/^[\u4e00-\u9fa5]{2,6}$/.test(value)){
                returnVal = true;
            }else{
                returnVal = false;
                return false
            }
            return this.WxValidate.optional(value) || returnVal
        }, '客户名称为2-6个中文')
        // 自定义验证规则
        this.WxValidate.addMethod('cardNo', (value, param) => {
            var returnVal = true;
            if(validateIdent.IdentityCode_isCardNo(value)){
                returnVal = true;
            }else{
                returnVal = false;
                return false
            }
            return this.WxValidate.optional(value) || returnVal
        }, '请输入18位身份证号')
        this.WxValidate.addMethod('checkProvince', (value, param) => {
            var returnVal = true;
            if(validateIdent.IdentityCode_checkProvince(value)){
                returnVal = true;
            }else{
                returnVal = false;
                return false
            }
            return this.WxValidate.optional(value) || returnVal
        }, '身份证号省份不正确')
        this.WxValidate.addMethod('checkBirthday', (value, param) => {
            var returnVal = true;
            if(validateIdent.IdentityCode_checkBirthday(value)){
                returnVal = true;
            }else{
                returnVal = false;
                return false
            }
            return this.WxValidate.optional(value) || returnVal
        }, '身份证号出生日期不正确')
        this.WxValidate.addMethod('checkParity', (value, param) => {
            var returnVal = true;
            if(validateIdent.IdentityCode_checkParity(value)){
                returnVal = true;
            }else{
                returnVal = false;
                return false
            }
            return this.WxValidate.optional(value) || returnVal
        }, '身份证号检验位不正确')
    },
    showModal(error) {
        wx.showToast({
            title: error.msg,
            icon: 'none',
            duration: 2000
        })
    },
})